﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;

namespace BSC_OTC_Management.Forms
{
    public partial class Frm_Duyet : Form
    {
        private string _ColumnName;
        private string _id;
        private DataTable _dt = new DataTable("thongtinsua");
        private string _function;
        private string _formName;
        private DataTable _tblView = new DataTable("viewMaintainLog");
        private Code.CL_Ultility _cl = new Code.CL_Ultility();

        public Frm_Duyet()
        {
            InitializeComponent();
        }

        public Frm_Duyet(string _ColumnName, string _id, DataTable _dt, string _function, string _formName)
        {
            // TODO: Complete member initialization
            InitializeComponent();

            //khoi tao doi tuong
            this._ColumnName = _ColumnName;
            this._id = _id;
            this._dt = _dt;
            this._function = _function;
            this._formName = _formName;

            //get tableView
            if (_function == Code.StaticVariable.function_Edit)
                _tblView = _cl._services.GetViewBy_SearchCode("BSC_MAINTAIN_LOG");
            else
                _tblView = _cl._services.GetViewBy_SearchCode(_formName);

            //show data
            ShowResult(_dt,_tblView);

        }

        private void ShowResult(DataTable _data, DataTable _view)
        {
 	        try{
                // Thay tên dựa theo FieldCode, FieldName
                this.dgvResult.DataSource = _data;
                

                foreach (DataGridViewColumn column in this.dgvResult.Columns)
                {
                    //autosize
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    // kiem tra, neu column datatype = decimal --> format kieu khac
                    if (column.ValueType == typeof(System.Decimal))
                    {
                        column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        column.DefaultCellStyle.Format = "#,##0.###";
                    }
                    foreach (DataRow row in _view.Rows)
                    {
                        if (row["fldname"].ToString() == column.HeaderText)
                        {
                            column.HeaderText = row["caption"].ToString();
                            column.HeaderCell.Style.Font = new Font("Tahoma", 8.25F, FontStyle.Bold);
                        }
                    }
                }
            }
            catch (Exception ex){
                MessageBox.Show(ex.Message, Code.StaticVariable.messeseboxLuuY);
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Approve_Click(object sender, EventArgs e)
        {
            try
            {
                //nếu duyệt thêm mới
                if (_function == Code.StaticVariable.function_AddNew)
                {
                    //duyet them moi
                    bool b = _cl._services.Approve_Data(_formName, _ColumnName, _id, (int)Code.StaticVariable.approveCase.DaDuyet);
                    if (b)
                    {
                        //lưu vết giao dịch
                        _cl._services.WritetoTLLOG("",
                                                    Code.StaticVariable.userID,
                                                    Code.StaticVariable.menu_Code,
                                                    "",
                                                    _cl.Current_DATE("dd/MM/yyyy"),
                                                    "",
                                                    _cl.Current_DATE("hh:mm:ss"),
                                                    (int)Code.StaticVariable.approveCase.DaDuyet + "",
                                                    Code.StaticVariable.function_Approve,
                                                    _id,
                                                    "");
                        //luu vet nguoi thuc hien
                        _cl._services.Update_Last_Approve(_formName, _ColumnName, _id, Code.StaticVariable.userID);
                        MessageBox.Show(Code.StaticVariable.messeseboxDuyetThanhCong, Code.StaticVariable.messeseboxLuuY);
                        this.Close();
                    }
                    else
                        MessageBox.Show(Code.StaticVariable.messeseboxDuyetThatBai, Code.StaticVariable.messeseboxLuuY);

                }

                //nếu duyệt sửa
                if (_function == Code.StaticVariable.function_Edit)
                {
                    // lay du lieu tu maintain_log --> 
                    string msg = this._cl._services.UpdateDataReal(_formName, _ColumnName, _id, Code.StaticVariable.userID);
                    //neu loi nem ra messagebox
                    if (msg != Code.StaticVariable.messeseboxSuaThanhCong)
                        throw new Exception(msg);
                    //sau khi update thanh cong , update truong duyet =1
                    bool b = _cl._services.Approve_Data(_formName, _ColumnName, _id, (int)Code.StaticVariable.approveCase.DaDuyet);
                    if (b)
                    {
                        //lưu vết giao dịch
                        _cl._services.WritetoTLLOG("",
                                                    Code.StaticVariable.userID,
                                                    Code.StaticVariable.menu_Code,
                                                    "",
                                                    _cl.Current_DATE("dd/MM/yyyy"),
                                                    "",
                                                    _cl.Current_DATE("hh:mm:ss"),
                                                    (int)Code.StaticVariable.approveCase.DaDuyet + "",
                                                    Code.StaticVariable.function_Approve,
                                                    _id,
                                                    "");
                        //luu vet nguoi thuc hien
                        _cl._services.Update_Last_Approve(_formName, _ColumnName, _id, Code.StaticVariable.userID);
                        MessageBox.Show(Code.StaticVariable.messeseboxDuyetThanhCong, Code.StaticVariable.messeseboxLuuY);
                        this.Close();
                    }
                    else
                        MessageBox.Show(Code.StaticVariable.messeseboxDuyetThatBai, Code.StaticVariable.messeseboxLuuY);
                }

                //nếu duyệt xóa
                if (_function == Code.StaticVariable.function_Delete)
                {
                    string result = _cl._services.DeleteRow(_formName, _id, _ColumnName, Code.StaticVariable.userID);
                    if (result == "OK")
                    {
                        //lưu vết giao dịch ??? xem lại
                        _cl._services.WritetoTLLOG("",
                                                    Code.StaticVariable.userID,
                                                    Code.StaticVariable.menu_Code,
                                                    "",
                                                    _cl.Current_DATE("dd/MM/yyyy"),
                                                    "",
                                                    _cl.Current_DATE("hh:mm:ss"),
                                                    (int)Code.StaticVariable.approveCase.DuyetXoa + "",
                                                    Code.StaticVariable.function_Approve,
                                                    _id,
                                                    "");
                        //luu vet nguoi thuc hien
                        _cl._services.Update_Last_Approve(_formName, _ColumnName, _id, Code.StaticVariable.userID);
                        MessageBox.Show(Code.StaticVariable.messeseboxThanhCong, this._formName, MessageBoxButtons.OK);
                        this.Close();
                    }
                    else
                        MessageBox.Show(result, this._formName, MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Code.StaticVariable.messeseboxLuuY);
            }
        }

        private void btn_Deny_Click(object sender, EventArgs e)
        {
            try
            {
                //nếu duyệt sửa thì  xóa maintain log đi, nếu xóa thì ko cần
                if (_function == Code.StaticVariable.function_Edit)
                {
                    //huy duyet --> xoa trong maintain log di
                    string _sqlwhere = _ColumnName + "_" + _id;
                    string sql = "delete bsc_maintain_log where record_key='" + _sqlwhere + "' and approve_time is null";
                    bool delete = this._cl._services.Excute_DB("delete bsc_maintain_log where record_key='" + _sqlwhere + "' and approve_time is null");
                    if (!delete)
                        throw new Exception("Không thể từ chối do lỗi xóa bảng maintain_log");
                }
                bool b = false;
                if(_function == Code.StaticVariable.function_AddNew)
                    //cap nhat  duyet o bang that = từ chối
                    b = _cl._services.Approve_Data(_formName, _ColumnName, _id, (int)Code.StaticVariable.approveCase.TuChoi);
                else
                    //cap nhat  duyet o bang that = đã duyệt
                    b = _cl._services.Approve_Data(_formName, _ColumnName, _id, (int)Code.StaticVariable.approveCase.DaDuyet);
                if (b)
                {
                    //lưu vết giao dịch
                    _cl._services.WritetoTLLOG("",
                                                Code.StaticVariable.userID,
                                                Code.StaticVariable.menu_Code,
                                                "",
                                                _cl.Current_DATE("dd/MM/yyyy"),
                                                "",
                                                _cl.Current_DATE("hh:mm:ss"),
                                                (int)Code.StaticVariable.approveCase.TuChoi + "",
                                                Code.StaticVariable.function_Approve,
                                                _id,
                                                "");
                    //luu vet nguoi thuc hien
                    _cl._services.Update_Last_Approve(_formName, _ColumnName, _id, Code.StaticVariable.userID);
                    MessageBox.Show(Code.StaticVariable.messeseboxThanhCong, Code.StaticVariable.messeseboxLuuY);
                    this.Close();
                }
                else
                    MessageBox.Show(Code.StaticVariable.messeseboxDuyetThatBai, Code.StaticVariable.messeseboxLuuY);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Code.StaticVariable.messeseboxLuuY);
            }
        }
    }
}
